from bible import bible, book, chapter, verse 
import shelve 
from nltk.stem.porter import PorterStemmer
import string

	
	
def primeText(text):
    text = text.translate(None,string.punctuation)
    text = text.lower()
    return [ret for ret in [stemmer.stem_word(x) for x in text.split()] if not ret  in stopwords]
			
def createIndex(loc,bible):
    keywords={}
    for bnum, book in bible.books.items():
        print(bnum)
        for cnum, chapter in book.chapters.items():
            for vnum,verse in chapter.verses.items():
                bookchap = (bnum,cnum,vnum)	
                for w in primeText(bible.bookNumToName[bnum]): 
                    addKeyword(keywords, bookchap,w)
                addKeyword(keywords, bookchap,str(cnum)+'c')
                addKeyword(keywords,bookchap,str(vnum)+'v')
                for word in primeText(verse.text):
                    addKeyword(keywords,bookchap,word)
    store = shelve.open(loc)  
    for k,v in  keywords.items():
        store[k]=v
    store.close()



def addKeyword(keywords, bookchap, word):
    if not word in keywords.keys():
        keywords[word] = {}
    if not bookchap in keywords[word].keys(): 
        keywords[word][bookchap] = 0
    keywords[word][bookchap] = keywords[word][bookchap]+1	


def lookup(loc,line):
    retset = None
    store = shelve.open(loc)
    keys = primeText(line)
    
    for k in keys:
        if k in store.keys():
            if retset ==None: 
                retset = {}
                d = store[k]
                for kk,kv in d.items():
                    retset[kk] = kv
            else:
                d = store[k]
                for kk,kv in d.items():
                    if kk in retset.keys():
                        retset[kk] = retset[kk] + 1
                dellist = []
                #kk are (b,c,v) value are counts
                for kk in retset.keys():
                    if not kk in d.keys():
                        dellist.append(kk)
                for i in dellist:
                    del retset[i]
    store.close()
    return retset
	
stopwordstxt = """
a
able
about
across
after
all
almost
also
am
among
an
and
any
are
as
at
be
because
been
but
by
can
cannot
could
dear
did
do
does
either
else
ever
every
for
from
get
got
had
has
have
he
her
hers
him
his
how
however
i
if
in
into
is
it
its
just
least
let
like
likely
may
me
might
most
must
my
neither
no
nor
not
of
off
often
on
only
or
other
our
own
rather
said
say
says
she
should
since
so
some
than
that
the
their
them
then
there
these
they
this
tis
to
too
twas
us
wants
was
we
were
what
when
where
which
while
who
whom
why
will
with
would
yet
you
your"""

stemmer = PorterStemmer()	
stopwords = set([stemmer.stem_word(x) for x in stopwordstxt.split('\n')])
